package com.newx.file.template.impl;

import cn.hutool.core.lang.Assert;
import com.newx.file.config.NewxFileConfig;
import com.newx.file.param.LocalFileUploadParam;
import com.newx.file.template.AbstractFileApiTemplate;
import com.newx.file.util.FileUploadUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yxl
 * @date 2024/7/22 0022 15:14
 */
@Service
@Slf4j
@AllArgsConstructor
public class LocalFileApiTemplateImpl extends AbstractFileApiTemplate {

    private NewxFileConfig newxFileConfig;
    @Override
    public Object uploadFile(MultipartFile file) {
        LocalFileUploadParam param = new LocalFileUploadParam();
        Assert.notBlank(newxFileConfig.getLocalFilePath(), "本地-文件存放路径未配置");
        Assert.notBlank(newxFileConfig.getLocaFileUrlPrefix(), "本地-文件访问前缀未配置");
        Assert.notBlank(newxFileConfig.getLocalBucket(), "本地-bucket未配置");
        param.setFile(file)
                .setFilePath(newxFileConfig.getLocalFilePath())
                .setFileUrlPrefix(newxFileConfig.getLocaFileUrlPrefix())
                .setBucket(newxFileConfig.getLocalBucket());
        return FileUploadUtil.uploadLocalFile(param);
    }
}
